package net.xdclass.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.model.LoginUser;
import java.util.Date;

@Slf4j
public class JWTUtil {
	public static final String subject = "xdclass";
	public static final String prefix  = "dcloud-link";
	public static final long  expiretime = 1000*60*60*24;
	public static final String scrict ="xdclass.net168";

	public static String getToken(LoginUser loginUser) {

		if(loginUser == null){
			throw new NullPointerException("传入对象为空");
		}
      String token = Jwts.builder().setSubject(subject)
			  .claim("accountNo",loginUser.getAccountNo())
			  .claim("headImg",loginUser.getHeadImg())
			  .claim("phone",loginUser.getPhone())
			  .claim("mail",loginUser.getMail())
			  .claim("username",loginUser.getUsername())
			  .claim("auth",loginUser.getAuth())
			  .setIssuedAt(new Date())
			  .setExpiration(new Date(CommonUtil.getCurrentTimestamp()+expiretime))
			  .signWith(SignatureAlgorithm.HS256,scrict).compact();
       token = prefix + token;
       return  token ;
	}

	public static Claims checkJWT(String token){
		try{
		Claims claims = Jwts.parser().setSigningKey(scrict)
				 .parseClaimsJws(token.replace(prefix,"")).getBody();
         return  claims;}
         catch (Exception e){
			log.info("token解析失败");
			return  null;
		 }
	}

}
